﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Web.Http;
using Monitor.Filters;
using Monitor.Models.JsonModel;
using Monitor.Repository.DataRepository.LinqModel;
using Car = Monitor.Repository.UserRepository.LinqModel.Car;
using Monitor.Repository.Development;
namespace Monitor.Controllers.ApiControllers
{
    public class CarsPositionController : MainApiController
    {
        #region GET 
        
        //[WebApiOutputCache(10, 10, true)]
        public Collection<CarInfo> Get([FromUri]IEnumerable<Int64> listOfId)
        {
            Collection<CarInfo> result = new Collection<CarInfo>();
            try
            {
                foreach (Int64 carId in listOfId.Distinct())
                {
                    Car car = CurrentUser.Cars.FirstOrDefault(n => n.CarId == carId);
                    if (car != null)
                    {
                        RegDataRow lastPosition = DataRepository
                            .MesuringRepository
                            .CoordRepository
                            .GetLastPosition(car.Fk_CarId);
                        CarInfo currentCarInfo = new CarInfo()
                                                     {
                                                         CarId = car.CarId,
                                                         CarName =
                                                             DataRepository.CarRepository.GetCarById(car.Fk_CarId).
                                                             CarName(),
                                                         CarComment =
                                                             DataRepository.CarRepository.GetCarById(car.Fk_CarId).
                                                             ClassifType.FullName,
                                                         CarCoords =
                                                             Coord.Parce(lastPosition.DataFromGPS.FirstOrDefault())
                                                     };
                        result.Add(currentCarInfo);
                    }
                }
            }
            catch (Exception ex)
            {
                ErrorLogger.Error(this.ControllerContext,ex);
            }
            return result;
        }

        #endregion
    }
}
